# code function to return daily averages
return_daily_avg <- function(data, abn_ret, cum_abn_ret, stratum = NULL, 
                             event_day = as.Date("2025-02-10"), size = 5, bizcal = business_calendar){
  est <- data %>%
    dplyr::filter(date >= offset(event_day, -size, bizcal) &
                    date <= offset(event_day, +size, bizcal)) %>%
    dplyr::group_by(across(c(all_of(stratum), date))) %>%
    dplyr::reframe(N = n(),
                   across(.cols = all_of(abn_ret), 
                          .fns = ~mean(.x, na.rm = TRUE),
                          .names = "avg_ar"),
                   across(.cols = all_of(abn_ret), 
                          .fns = ~sd(.x, na.rm = TRUE)/sqrt(N),
                          .names = "se_ar"),
                   across(.cols = all_of(cum_abn_ret), 
                          .fns = ~mean(.x, na.rm = TRUE),
                          .names = "avg_car"),
                   across(.cols = all_of(cum_abn_ret), 
                          .fns = ~sd(.x, na.rm = TRUE)/sqrt(N),
                          .names = "se_car")) %>%
    tidyr::pivot_longer(cols = c("avg_ar", "se_ar", "avg_car", "se_car"),
                        names_to = c(".value", "dv"),
                        names_pattern = "(avg|se)\\_(ar|car)") %>%
    dplyr::filter(!(dv == "car" & date < event_day)) %>%
    dplyr::group_by(across(c(all_of(stratum)))) %>%
    dplyr::mutate(N = unique(N[date == event_day])) %>%
    dplyr::ungroup() %>%
    dplyr::mutate(across(all_of(stratum),
                         ~as.character(.x)),
                  statistic = avg / se,
                  p.value = (1-pnorm(abs(statistic)))*2) %>%
    dplyr::rename("estimate" = "avg",
                  "std.error" = "se")
  
  if (!is.null(stratum)){
    est <- est %>%
      bind_rows(map(.x = unique(est$date),
                    .f = function(x){
                      d <- data %>%
                        dplyr::filter(date == as.Date(x, format = "%a, %b %d %Y")) %>%
                        dplyr::filter(if_any(.cols = all_of(abn_ret),
                                             .fns = ~!is.na(.x)))
                      
                      formula <- as.formula(paste(abn_ret, "~", stratum))
                      
                      t <- t.test(formula, data = d)
                      out <- data.frame(date = x,
                                        N = length(unique(d$ticker_symbol)),
                                        estimate = t$estimate[2]-t$estimate[1],
                                        std.error = t$stderr,
                                        statistic = t$statistic,
                                        p.value = t$p.value,
                                        stratum = "diff") %>%
                        dplyr::rename_with(.col = "stratum",
                                           .fn = ~stratum)
                      return(out)
                    }) %>%
                  bind_rows() %>%
                  as_tibble() %>%
                  mutate(dv = "ar") %>%
                  rbind(map(.x = unique(est$date[est$dv == "car"]),
                            .f = function(x){
                              d <- data %>%
                                dplyr::filter(date == as.Date(x, format = "%a, %b %d %Y")) %>%
                                dplyr::filter(if_any(.cols = all_of(cum_abn_ret),
                                                     .fns = ~!is.na(.x)))
                              formula <- as.formula(paste(cum_abn_ret, "~", stratum))
                              
                              t <- t.test(formula, data = d)
                              
                              out <- data.frame(date = x,
                                                N = length(unique(d$ticker_symbol)),
                                                estimate = t$estimate[2]-t$estimate[1],
                                                std.error = t$stderr,
                                                statistic = t$statistic,
                                                p.value = t$p.value,
                                                stratum = "diff") %>%
                                dplyr::rename_with(.col = "stratum",
                                                   .fn = ~stratum) 
                              return(out)
                            }) %>%
                          bind_rows() %>%
                          as_tibble() %>%
                          mutate(dv = "car")))
  }
  
  est <- est %>%
    dplyr::mutate(ci.lo = estimate - 1.96*std.error,
                  ci.hi = estimate + 1.96*std.error)
  
  return(est)
}



